package com.ruoyi.archive.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.archive.domain.SRetiredSoldier;
import com.ruoyi.archive.domain.SSoldierBaseInfo;
import com.ruoyi.archive.service.ISTransferredCadresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.archive.mapper.SRetiredCadreMapper;
import com.ruoyi.archive.domain.SRetiredCadre;
import com.ruoyi.archive.service.ISRetiredCadreService;

/**
 * 军队离休退休干部和退休士官信息Service业务层处理
 *
 * @author Allen
 * @date 2021-09-29
 */
@Service
public class SRetiredCadreServiceImpl extends ServiceImpl<SRetiredCadreMapper, SRetiredCadre> implements ISRetiredCadreService {
    @Autowired
    private SRetiredCadreMapper sRetiredCadreMapper;
    @Autowired
    private ISTransferredCadresService transferredCadresService;

    /**
     * 查询军队离休退休干部和退休士官信息
     *
     * @param id
     *         军队离休退休干部和退休士官信息主键
     *
     * @return 军队离休退休干部和退休士官信息
     */
    @Override
    public SRetiredCadre selectSRetiredCadreById(Long id) {
        return sRetiredCadreMapper.selectSRetiredCadreById(id);
    }

    /**
     * 查询军队离休退休干部和退休士官信息列表
     *
     * @param sRetiredCadre
     *         军队离休退休干部和退休士官信息
     *
     * @return 军队离休退休干部和退休士官信息
     */
    @Override
    public List<SRetiredCadre> selectSRetiredCadreList(SRetiredCadre sRetiredCadre) {
        List<SRetiredCadre> retiredCadreList = sRetiredCadreMapper.selectSRetiredCadreList(sRetiredCadre);
        if (CollectionUtil.isNotEmpty(retiredCadreList)) {
            List<Long> soldierIdList = retiredCadreList.stream().map(SRetiredCadre::getSoldierId).collect(Collectors.toList());
            Map<Long, SSoldierBaseInfo> baseInfoMap = transferredCadresService.getSoldierBaseInfoMap(soldierIdList);
            retiredCadreList.stream().forEach(x -> x.setSoldierName(baseInfoMap.get(x.getSoldierId()).getName()));
            retiredCadreList.stream().forEach(x -> x.setIdCard(baseInfoMap.get(x.getSoldierId()).getIdCard()));
        }
        return retiredCadreList;
    }

    /**
     * 新增军队离休退休干部和退休士官信息
     *
     * @param sRetiredCadre
     *         军队离休退休干部和退休士官信息
     *
     * @return 结果
     */
    @Override
    public int insertSRetiredCadre(SRetiredCadre sRetiredCadre) {
        if (sRetiredCadre != null){
            return sRetiredCadreMapper.insertSRetiredCadre(sRetiredCadre);
        }
        return 0;
    }

    /**
     * 修改军队离休退休干部和退休士官信息
     *
     * @param sRetiredCadre
     *         军队离休退休干部和退休士官信息
     *
     * @return 结果
     */
    @Override
    public int updateSRetiredCadre(SRetiredCadre sRetiredCadre) {
        return sRetiredCadreMapper.updateSRetiredCadre(sRetiredCadre);
    }

    /**
     * 批量删除军队离休退休干部和退休士官信息
     *
     * @param ids
     *         需要删除的军队离休退休干部和退休士官信息主键
     *
     * @return 结果
     */
    @Override
    public int deleteSRetiredCadreByIds(Long[] ids) {
        return sRetiredCadreMapper.deleteSRetiredCadreByIds(ids);
    }

    /**
     * 删除军队离休退休干部和退休士官信息信息
     *
     * @param id
     *         军队离休退休干部和退休士官信息主键
     *
     * @return 结果
     */
    @Override
    public int deleteSRetiredCadreById(Long id) {
        return sRetiredCadreMapper.deleteSRetiredCadreById(id);
    }
}
